﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Role.Entity;

namespace MvcMasterPage.Controllers
{
    public class ProductController : Controller
    {
        Role.Data.ProductRepository repository;
        Role.Data.WorkFlow_InfoRepository workFlow_InfoRepository;
        public ProductController()
        {
            repository = new Role.Data.ProductRepository();
            workFlow_InfoRepository = new Role.Data.WorkFlow_InfoRepository();
        }
        //
        // GET: /Product/

        public ActionResult Index()
        {
            var model = repository.GetDetailModel();
            return View(model);
        }

        public JsonResult ApplyWorkFlow(int productID, int nextWorkFlowInfoID)
        {
            JsonResult js = new JsonResult();
            try
            {
                repository.Update<Product>(i => new Product
                {
                    ID = productID,
                    WorkFlowID = nextWorkFlowInfoID,
                });
                var entity = repository.GetDetailModel().FirstOrDefault(i => i.ID == productID);
                js.Data = new
                {
                    res = true,
                    msg = (entity.WorkFlow_Info.PrevNodeEntity == null ? string.Empty
                    : entity.WorkFlow_Info.PrevNodeEntity.Name)
                    + "=>"
                    + "<b style='color: #f00'>" + entity.WorkFlow_Info.Name
                    + "</b>"
                    + (entity.WorkFlow_Info.NextNodeEntity == null ? string.Empty : "=>"
                    + entity.WorkFlow_Info.NextNodeEntity.Name)
                };
            }
            catch (Exception)
            {

                js.Data = new { res = false, href = "" };
            }

            return js;
        }

        /// <summary>
        /// 得到产品的播放地址
        /// </summary>
        /// <param name="productID"></param>
        /// <returns></returns>
        public JsonResult GetFilePath(int productID)
        {
            var js = new JsonResult();
            var entity = repository.GetEntity<Product>(i => i.ID == productID);
            if (entity != null)
            {
                js.Data = new
                {
                    res  = true,
                    path = entity.FilePath,
                };
            }
            else
            {
                js.Data = new
                   {
                       res  = false,
                       path = "",
                   };
            }
            js.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            return js;
        }
        //
        // GET: /Product/Details/5

        public ActionResult Details(int id)
        {
            var model = repository.GetEntity<Product>(i => i.ID == id);
            return View(model);
        }

        //
        // GET: /Product/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Product/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here   

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Product/Edit/5

        public ActionResult Edit(int id)
        {
            var model = repository.GetEntity<Product>(i => i.ID == id);
            return View(model);
        }

        //
        // POST: /Product/Edit/5

        [HttpPost]
        public ActionResult Edit(Product entity)
        {

            if (ModelState.IsValid)
            {
                repository.Update(entity);
                return RedirectToAction("Index");
            }

            return View();
        }

        //
        // GET: /Product/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Product/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
